package com.tms.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tms.entity.Book;
import com.tms.entity.Order;
import com.tms.entity.PageBean;
import com.tms.entity.Result;
import com.tms.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 存储订单的详细信息(Order)表控制层
 *
 * @author makejava
 * @since 2024-12-27 23:10:31
 */
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;


    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Order order) {
        return orderService.save(order) ? Result.success() : Result.error();
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Order order) {
        return orderService.updateById(order) ? Result.success() : Result.error();
    }

    //删除
    @DeleteMapping("/del")
    public Result del(@RequestParam String id) {
        return orderService.removeById(id) ? Result.success() : Result.error();
    }

    @GetMapping("/pageList")
    public Result pageList(@RequestBody PageBean pageBean) {
        HashMap params = pageBean.getParams();

        String userId = params.get("userId").toString();

        Page<Order> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());

        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(userId) && !"null".equals(userId)) {
            lambdaQueryWrapper.like(Order::getUserId, userId);
        }

        IPage result = orderService.pageList(page, lambdaQueryWrapper);

        return Result.success(result.getRecords());
    }

}

